home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / SceneCamera.cpp < prev    next >
C/C++ Source or Header  |  2005-10-26  |  8KB  |  249 lines

  1.  
  2. #include "EngineCore.h"
  3. #include "SceneCamera.h"
  4.  
  5. namespace peon
  6. {
  7.     SceneCamera::SceneCamera()
  8.     {
  9.     }
  10.  
  11.     SceneCamera::~SceneCamera()
  12.     {
  13.     }
  14.  
  15.     void SceneCamera::setPerspectiveProj( float fAspect, float z_min, float z_max )
  16.     {
  17.         glMatrixMode( GL_PROJECTION );
  18.         glLoadIdentity();
  19.         
  20.         gluPerspective( 45.0f, fAspect, z_min, z_max );
  21.  
  22.         glMatrixMode( GL_MODELVIEW );
  23.         glLoadIdentity();
  24.  
  25.     }
  26.  
  27.     void SceneCamera::setViewMatrix( Vector3& vecEye, Vector3& vecLookAt, Vector3& vecUp)
  28.     {
  29.     
  30.  
  31.         m_vecPos = vecEye;
  32.         m_vecUp = vecUp;
  33.         m_vecLookAt = vecLookAt;
  34.  
  35.  
  36.     }
  37.  
  38.     void SceneCamera::updateView()
  39.     {
  40.         
  41.         gluLookAt( m_vecPos.x, m_vecPos.y, m_vecPos.z,
  42.             m_vecLookAt.x, m_vecLookAt.y, m_vecLookAt.z,
  43.             m_vecUp.x, m_vecUp.y, m_vecUp.z );
  44.  
  45.     }
  46.  
  47.     void SceneCamera::onMouseEvent(SDL_Event* pEvent)
  48.     {
  49.         int middleX = EngineCore::getSingleton().getRenderer()->getWidth() >> 1;
  50.         int middleY = EngineCore::getSingleton().getRenderer()->getHeight() >> 1;
  51.  
  52.         if(pEvent->type == SDL_MOUSEMOTION )
  53.         {
  54.             int mousePos_x,mousePos_y;
  55.             //int middleX = SCREEN_WIDTH  >> 1;                // This is a binary shift to get half the width
  56.             //int middleY = SCREEN_HEIGHT >> 1;                // This is a binary shift to get half the height
  57.             float deltaY  = 0.0f;                            // This is the direction for looking up or down
  58.             float rotateY = 0.0f;                            // This will be the value we need to rotate around the Y axis (Left and Right)
  59.     
  60.             // Get the mouse's current X,Y position
  61.             SDL_GetMouseState(&mousePos_x,&mousePos_y);
  62.  
  63.             // If our cursor is still in the middle, we never moved... so don't update the screen
  64.             if( (mousePos_x == middleX) && (mousePos_y == middleY) ) return;
  65.  
  66.             // Set the mouse position to the middle of our window
  67.             SDL_WarpMouse(middleX, middleY);
  68.  
  69.             // Get the direction the mouse moved in, but bring the number down to a reasonable amount
  70.             rotateY = (float)( (middleX - mousePos_x) ) / 1000;        
  71.             deltaY  = (float)( (middleY - mousePos_y) ) / 1000;
  72.  
  73.             // Multiply the direction vVector for Y by an acceleration (The higher the faster is goes).
  74.             m_vecLookAt.y += deltaY * 8;
  75.  
  76.             // Check if the distance of our view exceeds 10 from our position, if so, stop it. (UP)
  77.             if( ( m_vecLookAt.y - m_vecPos.y ) >  10)  m_vecLookAt.y = m_vecPos.y + 10;
  78.  
  79.             // Check if the distance of our view exceeds -10 from our position, if so, stop it. (DOWN)
  80.             if( ( m_vecLookAt.y - m_vecPos.y ) < -10)  m_vecLookAt.y = m_vecPos.y - 10;
  81.  
  82.             // Here we rotate the view along the X avis depending on the direction (Left of Right)
  83.             rotateView(0, -rotateY, 0);
  84.  
  85.  
  86.         }
  87.     }
  88.  
  89.     void SceneCamera::rotateView(float X, float Y, float Z)
  90.     {
  91.         Vector3 vVector;                            // Vector for the position/view.
  92.  
  93.         // Get our view vVector (The direciton we are facing)
  94.         vVector.x = m_vecLookAt.x - m_vecPos.x;        // This gets the direction of the X    
  95.         vVector.y =    m_vecLookAt.y - m_vecPos.y;        // This gets the direction of the Y
  96.         vVector.z = m_vecLookAt.z - m_vecPos.z;        // This gets the direction of the Z
  97.  
  98.         // Rotate the view along the desired axis
  99.         if(X) 
  100.         {
  101.             // Rotate the view vVector up or down, then add it to our position
  102.             m_vecLookAt.z = (float)(m_vecPos.z + sin(X)*vVector.y + cos(X)*vVector.z);
  103.             m_vecLookAt.y = (float)(m_vecPos.y + cos(X)*vVector.y - sin(X)*vVector.z);
  104.         }
  105.         if(Y)
  106.         {
  107.             // Rotate the view vVector right or left, then add it to our position
  108.             m_vecLookAt.z = (float)(m_vecPos.z + sin(Y)*vVector.x + cos(Y)*vVector.z);
  109.             m_vecLookAt.x = (float)(m_vecPos.x + cos(Y)*vVector.x - sin(Y)*vVector.z);
  110.         }
  111.         if(Z)
  112.         {
  113.             // Rotate the view vVector diagnally right or diagnally down, then add it to our position
  114.             m_vecLookAt.x = (float)(m_vecPos.x + sin(Z)*vVector.y + cos(Z)*vVector.x);        
  115.             m_vecLookAt.y = (float)(m_vecPos.y + cos(Z)*vVector.y - sin(Z)*vVector.x);
  116.         }
  117.  
  118.  
  119.     }
  120.  
  121.     void SceneCamera::generateViewFrustum()
  122.     {
  123.  
  124.         /*
  125.         float p[16];   // projection matrix
  126.         float mv[16];  // model-view matrix
  127.         float mvp[16]; // model-view-projection matrix
  128.     
  129.         glGetFloatv( GL_PROJECTION_MATRIX, p );
  130.         glGetFloatv( GL_MODELVIEW_MATRIX, mv );
  131.  
  132.  
  133.  
  134.         //
  135.         // Concatenate the projection matrix and the model-view matrix to produce 
  136.         // a combined model-view-projection matrix.
  137.         //
  138.     
  139.         mvp[ 0] = mv[ 0] * p[ 0] + mv[ 1] * p[ 4] + mv[ 2] * p[ 8] + mv[ 3] * p[12];
  140.         mvp[ 1] = mv[ 0] * p[ 1] + mv[ 1] * p[ 5] + mv[ 2] * p[ 9] + mv[ 3] * p[13];
  141.         mvp[ 2] = mv[ 0] * p[ 2] + mv[ 1] * p[ 6] + mv[ 2] * p[10] + mv[ 3] * p[14];
  142.         mvp[ 3] = mv[ 0] * p[ 3] + mv[ 1] * p[ 7] + mv[ 2] * p[11] + mv[ 3] * p[15];
  143.  
  144.         mvp[ 4] = mv[ 4] * p[ 0] + mv[ 5] * p[ 4] + mv[ 6] * p[ 8] + mv[ 7] * p[12];
  145.         mvp[ 5] = mv[ 4] * p[ 1] + mv[ 5] * p[ 5] + mv[ 6] * p[ 9] + mv[ 7] * p[13];
  146.         mvp[ 6] = mv[ 4] * p[ 2] + mv[ 5] * p[ 6] + mv[ 6] * p[10] + mv[ 7] * p[14];
  147.         mvp[ 7] = mv[ 4] * p[ 3] + mv[ 5] * p[ 7] + mv[ 6] * p[11] + mv[ 7] * p[15];
  148.  
  149.         mvp[ 8] = mv[ 8] * p[ 0] + mv[ 9] * p[ 4] + mv[10] * p[ 8] + mv[11] * p[12];
  150.         mvp[ 9] = mv[ 8] * p[ 1] + mv[ 9] * p[ 5] + mv[10] * p[ 9] + mv[11] * p[13];
  151.         mvp[10] = mv[ 8] * p[ 2] + mv[ 9] * p[ 6] + mv[10] * p[10] + mv[11] * p[14];
  152.         mvp[11] = mv[ 8] * p[ 3] + mv[ 9] * p[ 7] + mv[10] * p[11] + mv[11] * p[15];
  153.  
  154.         mvp[12] = mv[12] * p[ 0] + mv[13] * p[ 4] + mv[14] * p[ 8] + mv[15] * p[12];
  155.         mvp[13] = mv[12] * p[ 1] + mv[13] * p[ 5] + mv[14] * p[ 9] + mv[15] * p[13];
  156.         mvp[14] = mv[12] * p[ 2] + mv[13] * p[ 6] + mv[14] * p[10] + mv[15] * p[14];
  157.         mvp[15] = mv[12] * p[ 3] + mv[13] * p[ 7] + mv[14] * p[11] + mv[15] * p[15];
  158.  
  159.         //
  160.         // Extract the frustum's right clipping plane and normalize it.
  161.         //
  162.  
  163.  
  164.         m_oFrustumPlanes[0].a = mvp[ 3] - mvp[ 0];
  165.         m_oFrustumPlanes[0].b = mvp[ 7] - mvp[ 4];
  166.         m_oFrustumPlanes[0].c = mvp[11] - mvp[ 8];
  167.         m_oFrustumPlanes[0].d = mvp[15] - mvp[12];
  168.  
  169.         m_oFrustumPlanes[0].normalize();
  170.  
  171.  
  172.         //
  173.         // Extract the frustum's left clipping plane and normalize it.
  174.         //
  175.  
  176.         m_oFrustumPlanes[1].a = mvp[ 3] + mvp[ 0];
  177.         m_oFrustumPlanes[1].b = mvp[ 7] + mvp[ 4];
  178.         m_oFrustumPlanes[1].c = mvp[11] + mvp[ 8];
  179.         m_oFrustumPlanes[1].d = mvp[15] + mvp[12];
  180.  
  181.         m_oFrustumPlanes[1].normalize();
  182.  
  183.         //
  184.         // Extract the frustum's bottom clipping plane and normalize it.
  185.         //
  186.  
  187.         m_oFrustumPlanes[2].a = mvp[ 3] + mvp[ 1];
  188.         m_oFrustumPlanes[2].b = mvp[ 7] + mvp[ 5];
  189.         m_oFrustumPlanes[2].c = mvp[11] + mvp[ 9];
  190.         m_oFrustumPlanes[2].d = mvp[15] + mvp[13];
  191.  
  192.         m_oFrustumPlanes[2].normalize();
  193.  
  194.         //
  195.         // Extract the frustum's top clipping plane and normalize it.
  196.         //
  197.  
  198.         m_oFrustumPlanes[3].a = mvp[ 3] - mvp[ 1];
  199.         m_oFrustumPlanes[3].b = mvp[ 7] - mvp[ 5];
  200.         m_oFrustumPlanes[3].c = mvp[11] - mvp[ 9];
  201.         m_oFrustumPlanes[3].d = mvp[15] - mvp[13];
  202.  
  203.         m_oFrustumPlanes[3].normalize();
  204.  
  205.  
  206.         //
  207.         // Extract the frustum's far clipping plane and normalize it.
  208.         //
  209.  
  210.         m_oFrustumPlanes[4].a = mvp[ 3] - mvp[ 2];
  211.         m_oFrustumPlanes[4].b = mvp[ 7] - mvp[ 6];
  212.         m_oFrustumPlanes[4].c = mvp[11] - mvp[10];
  213.         m_oFrustumPlanes[4].d = mvp[15] - mvp[14];
  214.  
  215.         m_oFrustumPlanes[4].normalize();
  216.  
  217.         //
  218.         // Extract the frustum's near clipping plane and normalize it.
  219.         //
  220.  
  221.         m_oFrustumPlanes[5].a = mvp[ 3] + mvp[ 2];
  222.         m_oFrustumPlanes[5].b = mvp[ 7] + mvp[ 6];
  223.         m_oFrustumPlanes[5].c = mvp[11] + mvp[10];
  224.         m_oFrustumPlanes[5].d = mvp[15] + mvp[14];
  225.  
  226.         m_oFrustumPlanes[5].normalize();
  227.         */
  228.  
  229.  
  230.     }
  231.  
  232.     bool SceneCamera::isSphereInFrustum( float x, float y, float z, float fRadius )
  233.     {
  234.         /*
  235.         for( int i = 0; i < 6; ++i )
  236.         {
  237.             if( m_oFrustumPlanes[i].a * x +
  238.                 m_oFrustumPlanes[i].b * y +
  239.                 m_oFrustumPlanes[i].c * z +
  240.                 m_oFrustumPlanes[i].d <= -fRadius )
  241.                 return false;
  242.         }
  243.         */
  244.  
  245.         return true;
  246.     }
  247.  
  248. }
  249.